﻿using System;
using System.Collections.Generic;
using Skynet.CommonLibrary.ThreadQueue.Interface;

namespace Skynet.CommonLibrary.ThreadQueue.Pool
{
    /// <summary>
    /// 数据池
    /// </summary>
    /// <typeparam name="T">通过类型查找 具体的池</typeparam>
    ///<remarks>
    /// 作者：         李志强
    /// 开发时间：  2011-04-18
    /// </remarks>
    public class DataPoolFactory<T>
    {
        static Dictionary<Type, IDataPool<T>> _poolDic;

        /// <summary>
        /// 添加一个数据池，如果重复则直接返回
        /// </summary>
        /// <param name="center">操作组件，当入队列时 会触发相应操作</param>
        public static void DataPoolInstance(IComponent center)
        {
            if (_poolDic == null)
            {
                _poolDic = new Dictionary<Type, IDataPool<T>>();
            }
            if (!_poolDic.ContainsKey(typeof(T)))
            {
                _poolDic.Add(typeof(T), new ReceiveDataPool<T>(Int32.MaxValue,center));
            }
        }

        /// <summary>
        /// 获取指定类型的数据池
        /// </summary>
        public static IDataPool<T> GetPool
        {
            get
            {
                if (_poolDic == null)
                {
                    _poolDic = new Dictionary<Type, IDataPool<T>>();
                }
                if (_poolDic.ContainsKey(typeof(T)))
                {
                    return _poolDic[typeof(T)];
                }
                return null;
            }
        }
    }
}
